package com.nondo.rdp.finance.endpoint.api.admin;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.rdp.auth.common.RoleUtils;
import com.nondo.rdp.auth.domain.Message;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.auth.endpoint.model.Query;
import com.nondo.rdp.auth.event.PushApplicationEvent;
import com.nondo.rdp.auth.service.MessageService;
import com.nondo.rdp.auth.service.UserMessageService;
import com.nondo.rdp.auth.service.UserService;
import com.nondo.rdp.core.common.BeanUtils;
import com.nondo.rdp.core.common.ValidateUtil;
import com.nondo.rdp.core.model.View;

@RestController("apiAdminMessage")
@RequestMapping("/api/admin/message")
public class MessageEndpoint {
	@Autowired
	MessageService messageService;
	
	@Autowired
	UserService userService;
	
	@Autowired
	UserMessageService userMessageService;
	
	@Autowired
    private ApplicationEventPublisher applicationEventPublisher;

	@RequestMapping("/info/{id}")
	public View info(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("id") Long id) {
		View<Message> view = new View<Message>(20000, "获取数据成功");
		
		Message message = null;
		if (id != null) {
			message = messageService.findOne(id);
		} else {
			message = new Message();
		}

		view.setData(message);
		return view;
	}

	@RequestMapping("/list")
	public View<Page> list(HttpServletRequest request,
			HttpServletResponse response, @RequestBody Query<Message> query) {
		View<Page> view = new View(20000, "获取列表成功");
		
		Message message = query.getData();
		if(message == null) {
			message = new Message();
			message.setType(0);
		}
		
		Page<Message> page = messageService.findAll(
				new Page<Message>(query.getPage(), query.getSize()), message);

		view.setData(page);

		return view;
	}

	@PreAuthorize("hasRole('ROLE_" + RoleUtils.ADMIN + "')")
	@RequestMapping("/add")
	public View add(HttpServletRequest request, HttpServletResponse response,
			@Validated @RequestBody Message message, BindingResult result) {
		View view = new View(20000, "添加成功");

		if (result.hasErrors()) {
			view.setCode(40000);
			view.setMessage(ValidateUtil.first(result.getAllErrors()));
		} else {
			message.setType(0);
			message.setMethod("message");
			applicationEventPublisher.publishEvent(new PushApplicationEvent(this, message));
		}
		return view;
	}

	@PreAuthorize("hasRole('ROLE_" + RoleUtils.ADMIN + "')")
	@RequestMapping("/update")
	public View update(HttpServletRequest request,
			HttpServletResponse response,
			@Validated @RequestBody Message message, BindingResult result) {
		View view = new View(20000, "更新成功");

		if (result.hasErrors()) {
			view.setCode(40000);
			view.setMessage(ValidateUtil.first(result.getAllErrors()));
		} else {
			Message messageDb = messageService.findOne(message.getId());
			BeanUtils.copy(message, messageDb);
			this.messageService.update(messageDb);
		}

		return view;
	}

	@PreAuthorize("hasRole('ROLE_" + RoleUtils.ADMIN + "')")
	@RequestMapping("/delete/{id}")
	public View delete(HttpServletRequest request,
			HttpServletResponse response, @PathVariable("id") Long id) {
		View view = new View(20000, "删除成功");
		
		this.messageService.delete(id);

		return view;
	}
}